﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// TeamMembers 类。
	/// </summary>
	public static partial class TeamMembers
	{
		public const string INFO_TYPE = "TeamMember";		// 信息的类别代码

		#region GetItem
		/// <summary>
		/// 根据标识获取指定的TeamMembers。
		/// </summary>
		/// <param name="o">TeamMembers集合。</param>
		/// <param name="teamId">获取的TeamMembers标识 TeamId。</param>
		/// <param name="userId">获取的TeamMembers标识 UserId。</param>
		/// <returns>获取的信息项。</returns>
		public static TeamMember GetItem(this System.Data.Linq.Table<TeamMember> o, int teamId, Guid userId)
		{
			return o.SingleOrDefault(p => p.TeamId == teamId && p.UserId == userId);
		}

		/// <summary>
		/// 根据标识获取指定的TeamMembers。
		/// </summary>
		/// <param name="teamId">获取的TeamMembers标识 TeamId。</param>
		/// <param name="userId">获取的TeamMembers标识 UserId。</param>
		/// <returns>获取的信息项。</returns>
		public static TeamMember GetItem(int teamId, Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.TeamMembers.GetItem(teamId, userId);
		}

		public static TeamMember GetLeader(this System.Data.Linq.Table<TeamMember> o, int teamId)
		{
			return o.SingleOrDefault(p => p.TeamId == teamId && p.IsLeader);
		}

		public static TeamMember GetLeader(int teamId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.TeamMembers.GetLeader(teamId);
		}

		public static int[] GetGropuIdsByLeader(this System.Data.Linq.Table<TeamMember> o, Guid leader)
		{
			var q = (from p in o
					 where p.UserId == leader && p.IsLeader && p.TeamBlack.IsEffective && !p.TeamBlack.IsDelete
					 select p.TeamId).Distinct().ToArray();
			return q;
		}

		public static int[] GetGropuIdsByLeader(Guid leader)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.TeamMembers.GetGropuIdsByLeader(leader);
		}	

		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的TeamMembers是否已经存在。
		/// </summary>
		/// <param name="o">TeamMembers集合。</param>
		/// <param name="teamId">验证的TeamMembers标识 TeamId。</param>
		/// <param name="userId">验证的TeamMembers标识 UserId。</param>
		/// <returns>布尔值表示指定的TeamMembers是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<TeamMember> o, int teamId, Guid userId)
		{
			return o.Any(p => p.TeamId == teamId && p.UserId == userId);
		}

		/// <summary>
		/// 验证指定的TeamMembers是否已经存在。
		/// </summary>
		/// <param name="teamId">验证的TeamMembers标识 TeamId。</param>
		/// <param name="userId">验证的TeamMembers标识 UserId。</param>
		/// <returns>布尔值表示指定的TeamMembers是否已经存在。</returns>
		public static bool Exists(int teamId, Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.TeamMembers.Exists(teamId, userId);
		}

		public static bool Exists(this System.Data.Linq.Table<TeamMember> o, int teamId)
		{
			return o.Any(p => p.TeamId == teamId && !p.TeamBlack.IsDelete && p.IsLeader);
		}

		public static bool Exists(int teamId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.TeamMembers.Exists(teamId);
		}


		private static bool Exists(this System.Data.Linq.Table<TeamMember> o,Guid userId, int? siteId)
		{
			if (siteId.HasValue)
				return o.Any(p => p.UserId == userId && p.IsLeader && !p.TeamBlack.IsDelete && p.TeamBlack.IsEffective && p.TeamBlack.SiteId == siteId.Value);
			
			return o.Any(p =>p.UserId == userId && p.IsLeader && !p.TeamBlack.IsDelete && p.TeamBlack.IsEffective);
		}

		public static bool Exists(Guid? userId, int? siteId)
		{
			if (!userId.HasValue) return false;
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.TeamMembers.Exists(userId.Value, siteId);
		}



		#endregion
	}



	/// <summary>
	/// TeamMembers 类操作部分。
	/// </summary>
	public static partial class TeamMembers
	{
		#region Insert
		/// <summary>
		/// 添加TeamMembers。
		/// </summary>
		/// <param name="insertedUserGroupUser">添加的TeamMembers。</param>
		/// <returns>添加的TeamMembers。</returns>
		public static TeamMember Insert(TeamMember insertedUserGroupUser)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			TeamMember teamMember = insertedUserGroupUser.ToNewEntity<TeamMember>();		

			GetInsertRight(teamMember);	// 权限验证

			dc.TeamMembers.InsertOnSubmit(teamMember);
			dc.SubmitChanges();			

			// 记录日志
			Logs.Insert(dc, TeamBlacks.ResourceId, (int)TeamBlacks.PermissionCode.Setting, INFO_TYPE, teamMember.TeamId + "-" + teamMember.UserId, "添加组员", teamMember.TeamId.ToString());

			// 返回只读数据
			return insertedUserGroupUser.FillByEntity(teamMember);
		}
		#endregion



		#region Update
		private static readonly HashSet<string> _updatedExcludeColumns = typeof(TeamMember).GetEntityColumnNames(new string[] {
				"TeamId",
				"UserId"
				// 在这里填写更新的排除列
			}, true);

		/// <summary>
		/// 修改TeamMembers。
		/// </summary>
		/// <param name="updatedUserGroupUser">修改的TeamMembers。</param>
		/// <returns>修改的TeamMembers。</returns>
		public static TeamMember Update(TeamMember updatedUserGroupUser)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			TeamMember teamMember = dc.TeamMembers.GetItem(updatedUserGroupUser.TeamId, updatedUserGroupUser.UserId);
			if (teamMember == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetInsertRight(teamMember);	// 权限验证

			teamMember.FillByEntity(updatedUserGroupUser, EntityColumnsSetType.Exclude, _updatedExcludeColumns);

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}		

			// 记录日志
			Logs.Insert(dc, TeamBlacks.ResourceId, (int)TeamBlacks.PermissionCode.Setting, INFO_TYPE, teamMember.TeamId + "-" + teamMember.UserId, "更新组员信息", teamMember.TeamId.ToString());

			// 返回只读数据
			return updatedUserGroupUser.FillByEntity(teamMember);
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除TeamMembers。
		/// </summary>
		/// <param name="teamId">删除的TeamMembers标识 TeamId。</param>
		/// <param name="userId">删除的TeamMembers标识 UserId。</param>
		public static void Delete(int teamId, Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			TeamMember teamMember = dc.TeamMembers.GetItem(teamId, userId);
			if (teamMember == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetDeleteRight(teamMember);	// 权限验证

			Delete(dc, teamMember);

			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, TeamBlacks.ResourceId, (int)TeamBlacks.PermissionCode.Setting, INFO_TYPE, teamMember.TeamId + "-" + teamMember.UserId,"移除组员", teamMember.TeamId.ToString());
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, TeamMember teamMember)
		{
			dc.TeamMembers.DeleteOnSubmit(teamMember);			
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, User parent)
		{
			var list = (from p in dc.TeamMembers
						where p.UserId == parent.UserId
						select p).ToList();
			foreach (var item in list)
				Delete(item.TeamId, item.UserId);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, TeamBlack parent)
		{
			IEnumerable<TeamMember> e = parent.TeamMembers.AsEnumerable();
			foreach (TeamMember i in e)
				Delete(dc, i);
		}
		#endregion
		#endregion
	}



	/// <summary>
	/// TeamMembers 类对应操作权限部分。
	/// </summary>
	public static partial class TeamMembers
	{
		#region GetInsertRight	
		/// <summary>
		/// 获取当前用户是否有添加TeamMembers的权限。
		/// </summary>
		/// <param name="teamMember">添加的TeamMembers对象。</param>
		/// <returns>布尔值表示当前用户是否有添加TeamMembers的权限。</returns>
		public static bool GetInsertRight(TeamMember teamMember)
		{
			return TeamBlacks.GetInsertRight(new TeamBlack { TeamId = teamMember.TeamId });
		}
		#endregion

		#region GetDeleteRight

		/// <summary>
		/// 获取当前用户是否有删除TeamMembers的权限。
		/// </summary>
		/// <param name="teamMember">删除的TeamMembers对象。</param>
		/// <returns>布尔值表示当前用户是否有删除TeamMembers的权限。</returns>
		public static bool GetDeleteRight(TeamMember teamMember)
		{
			return TeamBlacks.GetSettingRight(new TeamBlack { TeamId = teamMember.TeamId });
		}
		#endregion
	}	
}